//基本加载项
$(function () {
    $(document).on('click', '[data-pjax]', function (event) {
        alert("正在加载，请稍候...<i class='icon spinner loading '></i>");     
        var con =    $(this).data('pjax');
        var containerSelector = '#'+ (con && con!="true" ? con : 'mainContent');
        $.pjax.click(event, {
            container: containerSelector
        })
    });

    $(document).on('pjax:start', function () {
        NProgress.start();
    }).on('pjax:end', function () {
        NProgress.done();
    });

    $(document).on('click', '[data-dialog]', function (e) {
        e.preventDefault();
        var url = $(this).data("dialog") != "" ? $(this).data("dialog") : $(this).attr("href");
        var title = $(this).attr("title");
        var size = $(this).data("dialog-size");
        var id = $(this).attr("id") ? $(this).attr('id') : $(this).data("dialog-id");
        dialog({
            "title": title,
            "url": url,
            "size": size
        })
    });
});

var dialog = function (settings) {
    settings = $.extend({
        'close': true,
        'id': 'modal_ajax',
        'size': 'small', // mini,tiny,small,large,fullscreen
        'title': '',
        'icon': '',
        'url': '',
        'content': '',
        'basic': false,
        'actions': {},
    }, settings || {});

    var modalT = $("<div>", {
        "class": "ui modal " + settings.size,
        "id": settings.id
    });

    if (settings.basic) {
        modalT.addClass("basic");
    }

    if (settings.close) {
        modalT.append($("<i>", {
            "class": "close icon"
        }));
    }
    if (settings.title) {
        var header = $('<div>', {
            'class': "ui header",
            'html': settings.title
        })

        if (settings.icon) {
            header.addClass('icon').prepend($("<i>", {
                "class": "icon " + settings.icon
            }));
        }
        modalT.append(header);
    }

    var content = settings.url ?
        $("<div>", {
            "class": "scrolling content",
        }).load(settings.url) :
        $("<div>", {
            "class": "scrolling content",
            "html": settings.content
        });
    modalT.append(content);
    if (settings.actions) {
        var actions = $("<div>", {
            "class": "actions"
        });
        
        $.each(settings.actions, function (actionName, action) {           
            actions.append($("<div>", {
                "class": "ui button " + action.class,
                "text": action.name?action.name:actionName,
                "id":actionName
            }));
            
            $(document).off("click","#" + actionName)
            .on("click", "#" + actionName, function () {
                if (typeof action.action=="function") action.action();
                $("#" + settings.id).modal("hide").remove();
            });     
        
        });
        modalT.append(actions);
    }
    modalT.appendTo("body")
        .modal({
            "onHidden": function () {
                $("#" + settings.id).remove();
            }
        })
        .modal("show");
}

dialog.close = function (id) {
    $("#" + id).modal("hide");
}

var alert = function () {
    message = arguments[0];
    icon = arguments[1] ? arguments[1] : "info";
    $.toast({
        'heading': "信息",
        'position': 'top-center',
        'text': "<h2 class=\"center\">" + message + "</h2>",
        'showHideTransition': 'slide',
        'icon': icon
    })
}

var confirms = function (settings) {
    settings = $.extend({
        'close': false,
        'id': 'confirm_message',
        'size': 'small', // mini,tiny,small,large,fullscreen
        'title': '',
        'icon':'exclamation triangle',
        'content': '',
        'basic': true,
        'okBtn':{},
        'cancelBtn':{}
    }, settings || {});

    settings.content="<h2 class='ui center aligned red header'>"+settings.content+"</h2>";

    settings.actions = {
        'okBtn':{
            'class':settings.okBtn.class?settings.okBtn.class:"green",
            'name':settings.okBtn.name?settings.okBtn.name:"OK",
            'action':settings.okBtn.action
        },
        "cancelBtn":{
            'class':settings.cancelBtn.class?settings.cancelBtn.class:"teal",
            'name':settings.cancelBtn.name?settings.cancelBtn.name:'CANCEL',
            'action':settings.cancelBtn.action
        }     
    }
    dialog(settings);
}

var store = function () {
    if (typeof (arguments[0]) == 'object') {
        arguments[0].each(function (name, val) {
            localStorage.setItem(name, val);
        })
    } else if (arguments[1]) {
        localStorage.setItem(arguments[0], arguments[1]);
    } else {
        return localStorage.getItem(arguments[0]) ? localStorage.getItem(arguments[0]) : null;
    }
};

$.fn.imageLoad = function () {
    this.each(function () {
        var oT = $(this).offset().top;
        var sT = $(window).scrollTop();
        var cH = $(window).height();
        if (sT + cH >= oT) {
            var s = $(this).data('src');
            $(this).attr('src', s);
            $(this).data('isLoaded', 1);
        }
    });
}